import os
import subprocess
import time

# 定义ADB路径和设备ID（如果需要指定设备）
ADB_PATH = r"F:\Testenvironment\sdk\platform-tools\adb.exe" # 替换为你的ADB实际路径
DEVICE_ID = None  # 如果只有一个设备连接，可以设置为None，否则填入设备的ID

# 定义QQ APK文件路径
QQ_APK_PATH = r"F:\Testenvironment\comtencentmttx.apk"  # 替换为你的QQ APK实际路径

def run_adb_command(command):
    """运行ADB命令"""
    full_command = [ADB_PATH]
    if DEVICE_ID:
        full_command.extend(["-s", DEVICE_ID])
    full_command.extend(command)
    try:
        result = subprocess.run(full_command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        return result.stdout
    except subprocess.CalledProcessError as e:
        print(f"Error executing ADB command: {e.stderr}")
        return None

def install_qq():
    """安装QQ"""
    print("开始安装QQ...")
    result = run_adb_command(["install", QQ_APK_PATH])
    if result and "Success" in result:
        print("QQ安装成功！")
    else:
        print("QQ安装失败！")
    return "Success" in result

def main():
    # 检查设备是否连接
    print("检查设备连接状态...")
    devices = run_adb_command(["devices"])
    if devices and "device" in devices.splitlines()[1]:
        print("设备已连接！")
        # 安装QQ
        install_qq()
        # 等待几秒，确保安装完成
        time.sleep(5)
        # # 卸载QQ
        # uninstall_qq()
    else:
        print("设备未连接，请确保安卓虚拟机已启动并且通过USB调试连接到电脑。")

if __name__ == "__main__":
    main()